<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>cup.timeplus &#8212; cup 3.2.33 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/alabaster.css" />
    <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
    <script src="../../_static/doctools.js"></script>
    <script src="../../_static/sphinx_highlight.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
   
  <link rel="stylesheet" href="../../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for cup.timeplus</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/python</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="c1"># Copyright: [CUP] - See LICENSE for details.</span>
<span class="c1"># Authors: Guannan Ma (@mythmgn),</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">:desc:</span>
<span class="sd">    time related module. looking forward to accepting new patches</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">datetime</span>

<span class="kn">import</span> <span class="nn">pytz</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;get_str_now&#39;</span><span class="p">,</span> <span class="s1">&#39;TimePlus&#39;</span><span class="p">]</span>


<div class="viewcode-block" id="get_str_now"><a class="viewcode-back" href="../../cup.html#cup.timeplus.get_str_now">[docs]</a><span class="k">def</span> <span class="nf">get_str_now</span><span class="p">(</span><span class="n">fmt</span><span class="o">=</span><span class="s1">&#39;%Y-%m-</span><span class="si">%d</span><span class="s1">-%H-%M-%S&#39;</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    return string of &#39;now&#39;</span>

<span class="sd">    :param fmt:</span>
<span class="sd">        print-format, &#39;%Y-%m-%d-%H-%M-%S&#39; by default</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="n">fmt</span><span class="p">,</span> <span class="n">time</span><span class="o">.</span><span class="n">localtime</span><span class="p">()))</span></div>


<div class="viewcode-block" id="TimePlus"><a class="viewcode-back" href="../../cup.html#cup.timeplus.TimePlus">[docs]</a><span class="k">class</span> <span class="nc">TimePlus</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    TimePlus can convert printable time from 1 timezone 2 another including</span>
<span class="sd">    utc.</span>

<span class="sd">    Code Example:</span>
<span class="sd">    ::</span>

<span class="sd">        from __future__ import print_function</span>
<span class="sd">        from cup import timeplus</span>
<span class="sd">        import pytz</span>
<span class="sd">        tz_shanghai = pytz.timezone(&#39;Aisa/Shanghai&#39;)</span>
<span class="sd">        tplus = timeplus.TimePlus(tz_shanghai)</span>
<span class="sd">        dtobj = datetime.datetime.now()</span>
<span class="sd">        print(tplus.utc2local(dtobj))</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">timezone</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        initialize with timezone setup</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">timezone</span><span class="p">,</span> <span class="n">pytz</span><span class="o">.</span><span class="n">BaseTzInfo</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;not a object of pytz.timezone(&quot;xxx/xxx&quot;)&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_timezone</span> <span class="o">=</span> <span class="n">timezone</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_utc_tz</span> <span class="o">=</span> <span class="n">pytz</span><span class="o">.</span><span class="n">timezone</span><span class="p">(</span><span class="s1">&#39;UTC&#39;</span><span class="p">)</span>

<div class="viewcode-block" id="TimePlus.get_timezone"><a class="viewcode-back" href="../../cup.html#cup.timeplus.TimePlus.get_timezone">[docs]</a>    <span class="k">def</span> <span class="nf">get_timezone</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        return current pytz timezone object</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_timezone</span></div>

<div class="viewcode-block" id="TimePlus.set_newtimezone"><a class="viewcode-back" href="../../cup.html#cup.timeplus.TimePlus.set_newtimezone">[docs]</a>    <span class="k">def</span> <span class="nf">set_newtimezone</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">pytz_timezone</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        refresh timezone</span>

<span class="sd">        :return:</span>
<span class="sd">            True if refreshing is done. False otherwise</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_timezone</span> <span class="o">=</span> <span class="n">pytz_timezone</span></div>

<div class="viewcode-block" id="TimePlus.utc_now"><a class="viewcode-back" href="../../cup.html#cup.timeplus.TimePlus.utc_now">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">utc_now</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;return utc_now&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">(</span><span class="n">pytz</span><span class="o">.</span><span class="n">UTC</span><span class="p">)</span></div>

<div class="viewcode-block" id="TimePlus.local2utc"><a class="viewcode-back" href="../../cup.html#cup.timeplus.TimePlus.local2utc">[docs]</a>    <span class="k">def</span> <span class="nf">local2utc</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dateobj</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        local timezone to utc conversion</span>

<span class="sd">        :return:</span>
<span class="sd">            a datetime.datetime object with utc timezone enabled</span>

<span class="sd">        :raise:</span>
<span class="sd">            ValueError if dateobj is not a datetime.datetime object</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dateobj</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;dateobj is not a datetime.datetime&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">dateobj</span><span class="o">.</span><span class="n">astimezone</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_utc_tz</span><span class="p">)</span></div>

<div class="viewcode-block" id="TimePlus.utc2local"><a class="viewcode-back" href="../../cup.html#cup.timeplus.TimePlus.utc2local">[docs]</a>    <span class="k">def</span> <span class="nf">utc2local</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dateobj</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        utc datetime to local timezone datetime.datetime</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dateobj</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;dateobj is not a datetime.datetime&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">dateobj</span><span class="o">.</span><span class="n">astimezone</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_timezone</span><span class="p">)</span></div></div>


<span class="k">def</span> <span class="nf">sleep</span><span class="p">(</span><span class="n">seconds</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;sleep xx seconds; more accurate than time.sleep&quot;&quot;&quot;</span>
    <span class="n">start</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
    <span class="n">left</span> <span class="o">=</span> <span class="n">seconds</span>
    <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">left</span><span class="p">)</span>
        <span class="n">left</span> <span class="o">=</span> <span class="n">seconds</span> <span class="o">-</span> <span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">-</span> <span class="n">start</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">left</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">break</span>


<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">get_str_now</span><span class="p">())</span>
</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../../index.html">cup</a></h1>








<h3>Navigation</h3>
<p class="caption" role="heading"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../cup.html">cup package</a></li>
</ul>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../../index.html">Documentation overview</a><ul>
  <li><a href="../index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2023, CUP-DEV Team.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 7.0.1</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.13</a>
      
    </div>

    

    
  </body>
</html>